package site_announcement

import (
	"context"
	"errors"
	pb "git.mycaigou.com/gfyx/micro-gfyx-user-service/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/service/site_announcement"
)

type SiteAnnouncementServer struct {
	siteAnnouncementService site_announcement.SiteAnnouncementServiceIface
}

func NewSiteAnnouncementServer(siteAnnouncementService site_announcement.SiteAnnouncementServiceIface) pb.SiteAnnouncementServiceServer {
	return &SiteAnnouncementServer{siteAnnouncementService: siteAnnouncementService}
}

func (c *SiteAnnouncementServer) SaveSiteAnnouncement(ctx context.Context, request *pb.SaveSiteAnnouncementRequest) (*pb.SaveSiteAnnouncementResponse, error) {
	if request.Title == "" || request.Content == "" || request.Url == "" || request.Cycle == 0 || request.CreatedBy == "" {
		return nil, errors.New("参数为空，请检查参数输入")
	}
	resp, err := c.siteAnnouncementService.SaveSiteAnnouncement(ctx, request)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (c *SiteAnnouncementServer) GetSiteAnnouncementList(ctx context.Context, request *pb.GetSiteAnnouncementListRequest) (*pb.GetSiteAnnouncementListResponse, error) {
	list, err := c.siteAnnouncementService.GetSiteAnnouncementList(ctx, request)
	if err != nil {
		return nil, err
	}
	return list, err
}

func (c *SiteAnnouncementServer) GetSiteAnnouncementDetail(ctx context.Context, request *pb.GetSiteAnnouncementDetailRequest) (*pb.GetSiteAnnouncementDetailResponse, error) {
	if request.Id == 0 {
		return nil, errors.New("参数为空，请检查参数输入")
	}
	detail, err := c.siteAnnouncementService.GetSiteAnnouncementDetail(ctx, request)
	if err != nil {
		return nil, err
	}
	return detail, nil
}

func (c *SiteAnnouncementServer) DeleteSiteAnnouncement(ctx context.Context, request *pb.DeleteSiteAnnouncementRequest) (*pb.DeleteSiteAnnouncementResponse, error) {
	if request.Id == 0 {
		return nil, errors.New("参数为空，请检查参数输入")
	}
	resp, err := c.siteAnnouncementService.DeleteSiteAnnouncement(ctx, request)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (c *SiteAnnouncementServer) SetSiteAnnouncementRead(ctx context.Context, request *pb.SetSiteAnnouncementReadRequest) (*pb.SetSiteAnnouncementReadResponse, error) {
	if request.SiteAnnouncementId == 0 || request.UserId == 0 || request.TenantCode == "" {
		return nil, errors.New("参数为空，请检查参数输入")
	}
	resp, err := c.siteAnnouncementService.SetSiteAnnouncementViewRecord(ctx, request)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (c *SiteAnnouncementServer) GetUserSiteAnnouncementList(ctx context.Context, request *pb.GetUserSiteAnnouncementListRequest) (*pb.GetUserSiteAnnouncementListResponse, error) {
	if request.UserId == 0 || request.TenantCode == "" {
		return nil, errors.New("参数为空，请检查参数输入")
	}
	resp, err := c.siteAnnouncementService.GetUserSiteAnnouncementList(ctx, request)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
